package 每日一题;

/**
 * @author 会玩的洋洋
 * https://leetcode-cn.com/problems/zigzag-conversion/
 */
public class _6_Z字形变换 {
    public String convert(String s, int numRows) {
        int n = s.length();
        char[][] target = new char[numRows][n >> 1];
        boolean flag = false;
        int row = 0, col = 0;
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            if (row == numRows - 1) {
                flag = true;
            } else if (row == 0) {
                flag = false;
            }
            target[row][col] = c;
            if (flag) {
                row--;
                col++;
            } else {
                row++;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < (n >> 1); j++) {
                char c = target[i][j];
                if (c != 0) {
                    sb.append(c);
                }
            }
        }
        return sb.toString();
    }
}
